swadhinbiswas # Changelog · 1/17/2026
1464862
44 lines · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getStorage } from "@/lib/storage";
import { verifyStorageSignature } from "@/lib/storage-auth";
import type { APIRoute } from "astro";
import { Readable } from "stream";

export const PUT: APIRoute = async ({ params, request, url }) => {
  const { key } = params;
  if (!key) return new Response("Missing key", { status: 400 });

  // Verify signature for secure access
  const signature = url.searchParams.get("sig");
  const expires = url.searchParams.get("exp");

  if (!verifyStorageSignature(key, signature, expires)) {
    return new Response("Unauthorized - Invalid or expired signature", { status: 401 });
  }

  // Removed duplicate storage declaration

  if (!request.body) return new Response("Body required", { status: 400 });

  try {
    // Convert Web ReadableStream to Node Readable
    const reader = request.body.getReader();
    const nodeReadable = new Readable({
      async read() {
        const { done, value } = await reader.read();
        if (done) {
          this.push(null);
        } else {
          this.push(Buffer.from(value));
        }
      },
    });

    const storage = await getStorage();
    await storage.put(key, nodeReadable);
    return new Response("OK", { status: 200 });
  } catch (e) {
    console.error(e);
    return new Response("Upload failed", { status: 500 });
  }
};